unit CladrFrm;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db, dbf, FileUtil, Forms, Controls, Graphics, Dialogs,
  DBGrids, DbCtrls, StdCtrls,LazUTF8,commonetypes;

type

  { TCladrForm }

  TCladrForm = class(TForm)
    Button1: TButton;
    Datasource1: TDatasource;
    Dbf1: TDbf;
    Dbf1FORMALNAME: TStringField;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    function Dbf1Translate(Dbf: TDbf; Src, Dest: PChar; ToOem: Boolean
      ): Integer;
    procedure FormCreate(Sender: TObject);
    procedure ToggleBox1Change(Sender: TObject);
    procedure OnGetText(Sender: TField; var aText: string;
    DisplayText: Boolean);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  CladrForm: TCladrForm;

implementation

uses consolefrm;

{$R *.lfm}

{ TCladrForm }

procedure TCladrForm.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Label1.Caption:=IntToStr(Dbf1.CodePage);
  for i := 0 to Dbf1.FieldCount-1 do
      If Dbf1.Fields[i].DataType = ftString then
         begin
         TStringField(Dbf1.Fields[i]).OnGetText:=@OnGetText;
         TStringField(Dbf1.Fields[i]).Transliterate:=True;
         end;
end;

procedure TCladrForm.ToggleBox1Change(Sender: TObject);
begin

end;

procedure TCladrForm.OnGetText(Sender: TField; var aText: string;
  DisplayText: Boolean);
begin
  if Sender.Value = Null then exit;
     aText:=AnsiToUtf8(Sender.Value);
end;

function TCladrForm.Dbf1Translate(Dbf: TDbf; Src, Dest: PChar; ToOem: Boolean
  ): Integer;
var
S:String;
i: Integer;
begin
  S:='';
  for i:= 0 to Length(String(Src))-1 do
      begin
        S:=S+Cp866ToCp1251Char(Src[i]);
      end;
Label1.Caption:=AnsiToUtf8(S);
Dest:=PChar(S);
//  ShowMessage('OnTransl '+BoolToStr(ToOem,'True','False'));
end;

procedure TCladrForm.Button1Click(Sender: TObject);
begin
  Dbf1.Translate('','',True);
end;

end.

